package com.yl.googleplayer_yl.ui.activity;

import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.yl.googleplayer_yl.R;
import com.yl.googleplayer_yl.adapter.MainAdapter;
import com.yl.googleplayer_yl.ui.widget.PagerSlidingTab;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends ActionBarActivity {
    private static final String TAG = "MainActivity";
    @BindView(R.id.pst_tab)
    PagerSlidingTab pstTab;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.drawerLayout)
    DrawerLayout drawerLayout;
    private MainAdapter mainAdapter;
    private ActionBar bar;
    private ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();

        setActionBar();


    }


    /**
     * 点击ActionBar的home按钮会执行该方法，所以在该方法中做判断*
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                toggle.onOptionsItemSelected(item);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void setActionBar() {
        bar = getSupportActionBar();
        bar.setTitle("谷歌电子市场");
        bar.setIcon(R.drawable.ic_launcher);
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setDefaultDisplayHomeAsUpEnabled(true);//设置home键可用被点击
        bar.setDisplayShowHomeEnabled(true);
        //将home键的返回图标设置为汉堡包图标
        toggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer_am, 0, 0);
        toggle.syncState();
        drawerLayout.setDrawerListener(toggle);
    }

    private void initView() {
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mainAdapter = new MainAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mainAdapter);
        pstTab.setViewPager(viewPager);
    }
}
